package com.jumi.microservice.controller.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jm.member.common.core.utils.ServletUtils;
import com.jm.member.common.jwt.constant.JwtAdminInfo;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.dto.reponse.api.InviterResponse;
import com.jumi.microservice.entity.JmQrcode;
import com.jumi.microservice.entity.JmUser;
import com.jumi.microservice.entity.JmUserInvite;
import com.jumi.microservice.entity.vo.JmUserInviterVo;
import com.jumi.microservice.enums.SourceType;
import com.jumi.microservice.service.IJmQrcodeService;
import com.jumi.microservice.service.IJmUserInviteService;
import com.jumi.microservice.service.IJmUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/inviter")
@Api(tags = "C-我的邀请人")
public class JmInviterController {
    @Autowired
    IJmUserService userService;

    @Autowired
    IJmUserInviteService inviteService;

    @Autowired
    IJmQrcodeService qrcodeService;

    @GetMapping("/myInviter")
    @ApiOperation(value = "我的邀请人-已绑定查询")
    public ResponseResult myInvite() {
        Integer userId = ServletUtils.getIntHeader(JwtAdminInfo.apiUserId);
        JmUser jmUser = userService.checkUserExist(userId);//检查个人信息
        JmUser inviteUser = userService.checkUserExist(jmUser.getInviterId());//检查上级是否存在
        JmUserInvite invite = inviteService.getInviteByUid(jmUser.getId());//获得上级信息
        if (ObjectUtil.isNotEmpty(invite)) {
            JmQrcode qrcode = qrcodeService.getQrcodeByUid(inviteUser.getId());//获取二维码
            InviterResponse response=new InviterResponse();
            BeanUtils.copyProperties(inviteUser,response);
            BeanUtils.copyProperties(invite,response);
            response.setCreateTime(invite.getCreateTime());
            if(ObjectUtil.isNotEmpty(qrcode)){
                String qr=qrcode.getPersonalQr();
                response.setQr(qr);
            }
            return ResponseResult.success(response);
        }
        return ResponseResult.error(500, "暂未绑定邀请人");
    }

    @GetMapping("/getMemberByCode/{code}")
    @ApiOperation(value = "我的邀请人-未绑定查询")
    @ApiImplicitParam(name="code",value = "邀请人邀请码")
    public ResponseResult getMemberByUid(@PathVariable String code) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("code", code);
        wrapper.select("avatar", "name", "truename", "id", "code");
        JmUser one = userService.getOne(wrapper);
        Map<String, Object> map = BeanUtil.beanToMap(one);
        JmUserInviterVo vo=new JmUserInviterVo();
        BeanUtil.copyProperties(map,vo);
        if (ObjectUtil.isNotEmpty(vo)) {
            return ResponseResult.success(vo);
        }
        return ResponseResult.error(HttpStatus.HTTP_NOT_FOUND, "未查询到该邀请人");
    }

    @GetMapping("/bindInviter")
    @ApiOperation(value = "我的邀请人-绑定邀请人")
    @Transactional
    public ResponseResult bindInviter(@ApiParam(value = "我的邀请人id") @RequestParam Integer uid) {
        Integer memberId = ServletUtils.getIntHeader(JwtAdminInfo.apiUserId);
        JmUser inviter = userService.getById(uid);
        JmUser I = userService.getById(memberId);
        userService.checkBindUser(I, inviter, 0);
        return userService.bindUp(I, inviter, SourceType.填写邀请码);
    }
}
